//
//  LRTabBar.swift
//  LRWeibo
//
//  Created by 雷路荣 on 15/12/15.
//  Copyright © 2015年 leilurong. All rights reserved.
//

import UIKit

class LRTabBar: UITabBar {
    
    //MARK: - 闭包属性
    /// 按钮点击的闭包 (() -> ())? 闭包是一个可选
    var composeClosure: (() -> ())?
    
    //私有常量
    //private let count = 5
    override func layoutSubviews() {
        super.layoutSubviews()
        //print("\(self.frame)")
        let count = self.items!.count + 1//加号按钮所以加1
        let width = self.bounds.width/CGFloat(count)
        let frame = CGRect(x: 0, y: 0, width: width, height: self.bounds.height)
        var index = 0
        for view in subviews {
            
/*    print("view: \(view)")
view: <_UITabBarBackgroundView: 0x7fda82f11000; frame = (0 0; 414 49); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fda82f8df50>>
view: <UITabBarButton: 0x7fda85015650; frame = (2 1; 100 48); opaque = NO; layer = <CALayer: 0x7fda85016200>>
view: <UITabBarButton: 0x7fda82e8eff0; frame = (106 1; 99 48); opaque = NO; layer = <CALayer: 0x7fda82e8ef20>>
view: <UITabBarButton: 0x7fda82d9b540; frame = (209 1; 100 48); opaque = NO; layer = <CALayer: 0x7fda85017090>>
view: <UITabBarButton: 0x7fda82e93790; frame = (313 1; 99 48); opaque = NO; layer = <CALayer: 0x7fda85013db0>>
view: <UIImageView: 0x7fda82f17450; frame = (0 -0.333333; 414 0.333333); autoresize = W; userInteractionEnabled = NO; layer = <CALayer: 0x7fda82fb6f60>>
view: <UIButton: 0x7fda82f17b20; frame = (165.6 0; 82.8 49); opaque = NO; layer = <CALayer: 0x7fda82f8da40>>
*/
            // 设置 UITabBarButton 的frame UITabBarButton 继承UIContorl
            // is: 判断类型
            if view is UIControl && !(view is UIButton){//不是UIButton
                //设置每一个按钮frame   CGRectOffset:  CGRect偏移
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
//                index++
//                if index == 2{//开一个位置给加号按钮
//                    index++
//                }
                index += index == 1 ? 2 : 1
            }
            //设置加号按钮frame
            composeButton.frame = CGRectOffset(frame, 2 * width, 0)
        }
    }
    //加号按钮点击
    func composeButtonClick() {
        //print("+号按钮点击")
        //执行闭包
        composeClosure?()
    }
    
    //懒加载加号按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "composeButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        // 添加到父控件上面 在代码块不能省略self
        self.addSubview(button)
        return button
    }()

}
